package com.acce.gxsc.page.registerCarRecord.child;

import com.acce.gxsc.model.WorkModel;
import com.acce.gxsc.model.loaclBean.PregisterListBean;
import com.acce.gxsc.model.loaclBean.ResultBean;
import com.acce.gxsc.model.netBean.CarPredictionBean;
import com.acce.sdk.base.mvp.BasePresenter;
import com.acce.sdk.utils.ToastUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Subscriber;
import rx.functions.Func1;

/**
 * Created by huxley on 2017/5/26.
 */

public class RegisterCarRecordChildPresenter extends BasePresenter<RegisterCarRecordChildView> {

    String  time;
    boolean isLoadOver;

    public void loadRegisterCarInfo(final boolean isLoadMore, String preContainNo, String pregisterBillState, String consignorNick, String declareCompanyNick) {
        if (mViewRef != null && isLoadMore && isLoadOver) {
            getView().setRefreshing(false);
            return;
        }
        if (!isLoadMore) {
            isLoadOver = false;
            time = "";
        }
        WorkModel.getInstance().loadRegisterCarList(time, preContainNo, pregisterBillState, declareCompanyNick, consignorNick)
                .map(new Func1<ResultBean<PregisterListBean>, ArrayList<CarPredictionBean>>() {
                    @Override
                    public ArrayList<CarPredictionBean> call(ResultBean<PregisterListBean> pregisterListBeanResultBean) {
                        if (pregisterListBeanResultBean == null || pregisterListBeanResultBean.result == null || pregisterListBeanResultBean.result.PregisterList == null || pregisterListBeanResultBean.result.PregisterList.size() <= 0) {
                            return new ArrayList<>();
                        }
                        return convertHandoverList(isLoadMore, pregisterListBeanResultBean.result.PregisterList);
                    }
                })
                .subscribe(new Subscriber<ArrayList<CarPredictionBean>>() {
                    @Override
                    public void onStart() {
                        if (mViewRef != null) {
                            getView().setRefreshing(true);
                        }
                    }

                    @Override
                    public void onNext(ArrayList<CarPredictionBean> datas) {
                        List<CarPredictionBean> listBatchList = datas;
                        if (listBatchList != null && listBatchList.size() > 0) {
                            RegisterCarRecordChildPresenter.this.time = listBatchList.get(listBatchList.size() - 1).child.get(listBatchList.get(listBatchList.size() - 1).child.size() - 1).createTime;
                        } else {
                            RegisterCarRecordChildPresenter.this.isLoadOver = true;
                            if (isLoadMore) {
                                ToastUtils.show("没有更多数据");
                                return;
                            }
                        }
                        if (mViewRef != null) {
                            getView().loadRegisterCarInfoSuccess(datas);
                        }
                    }

                    @Override
                    public void onCompleted() {
                        if (mViewRef != null) {
                            getView().setRefreshing(false);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (mViewRef != null) {
                            e.printStackTrace();
                            getView().setRefreshing(false);
                        }
                    }
                });
    }


    HashMap<Integer, ArrayList<CarPredictionBean>> handoverListMap;

    private ArrayList<CarPredictionBean> convertHandoverList(boolean isLoadMore, List<CarPredictionBean> saleSheets) {
        if (!isLoadMore) {
            handoverListMap = new HashMap<>();
        }
        for (int i = 0; i < saleSheets.size(); i++) {
            CarPredictionBean saleSheet = saleSheets.get(i);
            int key = Integer.valueOf(saleSheet.createTime.substring(0, 10).replaceAll("-", ""));
            if (handoverListMap.containsKey(key)) {
                handoverListMap.get(key).add(saleSheet);
            } else {
                ArrayList<CarPredictionBean> ssList = new ArrayList<>();
                ssList.add(saleSheet);
                handoverListMap.put(key, ssList);
            }
        }
        ArrayList<Map.Entry<Integer, ArrayList<CarPredictionBean>>> infoIds = new ArrayList<>(handoverListMap.entrySet());
        Collections.sort(infoIds, new Comparator<Map.Entry<Integer, ArrayList<CarPredictionBean>>>() {
            public int compare(Map.Entry<Integer, ArrayList<CarPredictionBean>> o1, Map.Entry<Integer, ArrayList<CarPredictionBean>> o2) {
                return o2.getKey() - o1.getKey();
            }
        });
        ArrayList<CarPredictionBean> ssrList = new ArrayList<>();
        for (int i = 0; i < infoIds.size(); i++) {
            ArrayList<CarPredictionBean> ssrs = handoverListMap.get(infoIds.get(i).getKey());
            CarPredictionBean ssr = new CarPredictionBean();
            ssr.createTime = ssrs.get(0).createTime.substring(0, 10);
            ssr.child = ssrs;
            ssrList.add(ssr);
        }
        return ssrList;
    }
}
